package com.yjz.common.extend

import android.text.TextUtils
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import com.yjz.common.util.AppGlobals

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/2/14 14:46
 */
fun AppCompatActivity.getResStr(@StringRes id: Int): String {
    return this.resources.getString(id)
}

fun AppCompatActivity.back() {
    onBackPressedDispatcher.onBackPressed()
}

fun AppCompatActivity.getResColor(@ColorRes id: Int): Int {
    return ContextCompat.getColor(this, id)
}

fun Fragment.getResStr(@StringRes id: Int): String {
    return this.resources.getString(id)
}

fun Fragment.getResColor(@ColorRes id: Int): Int {
    return ContextCompat.getColor(context!!, id)
}


fun AppCompatActivity.showToast(message: String?) {
    if (TextUtils.isEmpty(message)) return
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

fun AppCompatActivity.showToast(@StringRes id: Int) {
    try {
        val str = getResStr(id)
        if (TextUtils.isEmpty(str)) return
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show()
    } catch (err: Exception) {
        err.printStackTrace()
    }
}

fun ViewModel.showToast(message: String?) {
    if (TextUtils.isEmpty(message)) return
    Toast.makeText(AppGlobals.get()!!, message, Toast.LENGTH_SHORT).show()
}

fun Fragment.showToast(message: String?) {
    if (TextUtils.isEmpty(message)) return
    Toast.makeText(context!!, message, Toast.LENGTH_SHORT).show()
}

fun Fragment.showToast(@StringRes id: Int) {
    try {
        val str = getResStr(id)
        if (TextUtils.isEmpty(str)) return
        Toast.makeText(context!!, str, Toast.LENGTH_SHORT).show()
    } catch (err: Exception) {
        err.printStackTrace()
    }
}






